<?php
/**
 * Created by PhpStorm.
 * User: andrei
 * Date: Jun 27, 2012
 * Time: 3:33:54 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Db_Products_PhotosGalleries extends Common {
	protected static $tableName = TAB_PHOTO_GALLERIES;
	protected static $dbFields = array(
        'id',
        'pg_name',
        'pg_gallery_type',
        'pg_owner_id',
        'pg_user_id',
        'pg_date',
        'deleted'
	);

	/**
	 * @var Db_Products_PhotosGalleries
	 */
	public $objectArray;

	public $id;
	public $pg_name;
	public $pg_gallery_type;
	public $pg_owner_id;
	public $pg_user_id;
    public $deleted;

    function __construct($value=NULL, $fieldName=NULL) {
        parent::__construct($value, $fieldName);
    }

	public static function getGalleriesForSelect() {
		$sql = "SELECT `id`, `pg_name` FROM ?f WHERE `deleted` = 0 AND `pg_gallery_type` = ? ;";

		$photoGallery = self::$DB->q($sql, self::$tableName, 'default');
		$dataReturn = array();

		while($data = $photoGallery->fetch_object()) {
			$dataReturn[$data->id] = $data->pg_name;
		}

		return $dataReturn;
	}

	/**
	 * @static
	 * @param $photoGalleryIdentifier
	 * @return array|bool
	 */
	public static function getPhotosGalleryByMemoryListIdentifier($photoGalleryIdentifier) {
		if(!$photoGalleryIdentifier) {
			return false;
		}
		//return $photoGroupId;
		$allPhotoGallery = self::$DB->q("SELECT `pg`.`pg_session_photo_group_id`, `pg`.`pg_gallery_type`, `ph`.* FROM ?f `pg`
										 INNER JOIN ?f `ph` ON `pg`.`id` = `ph`.`ph_gallery_id`
										 INNER JOIN ?f `ml` ON `ml`.`ml_gallery_id` = `pg`.`id`
										 WHERE `ml`.`ml_memory_identifier` = ? AND `pg`.`deleted` = 0 AND `ph`.`deleted` = 0 AND `ml`.`deleted` = 0 ;",
										 self::$tableName, TAB_PHOTOS, TAB_MEMORY_LIST, $photoGalleryIdentifier);
		$dataReturn = array();
		$env = conf('ENVIRONMENT');
		while ($data = $allPhotoGallery->fetch_assoc()) {
			$data['ph_original'] = self::$PROJECT['pr_resources_'. $env].$data['ph_path'].DS.$data['ph_name'];
			$data['ph_resized'] = self::$PROJECT['pr_resources_'. $env].$data['ph_path'].DS.$data['ph_resized'];
			$dataReturn[] = $data;
		}
		return $dataReturn;
	}

	/**
	 * @param $galleryId
	 * @param string $galleryType
	 * @return array|bool
	 */

	public function getGalleryImagesById($galleryId, $galleryType = 'user') {
		if(!$galleryId) {
			return false;
		}
		$galleryImages = self::$DB->q("SELECT `pg`.*, `pg`.`id` as `gallery_id`, `ph`.* ,`ph`.`id` as `photo_id` FROM ?f `pg`
										INNER JOIN ?f `ph` ON `pg`.`id` = `ph`.`ph_gallery_id`
										WHERE `pg`.`id` = ?d AND `pg`.`deleted` = 0 AND `ph`.`deleted` = 0 AND `pg`.`pg_gallery_type` = ?  ORDER BY `ph`.`id` DESC;",
										self::$tableName, TAB_PHOTOS, $galleryId, $galleryType);
		$dataReturn = array();
		$env = conf('ENVIRONMENT');
		while ($data = $galleryImages->fetch_object()) {
			$data->ph_original = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_name;
			$data->ph_resized = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_resized;
			$dataReturn[$data->photo_id] = $data;
		}
		return $dataReturn;
	}

	/**
	 * @static
	 * @param $galleryId
	 * @param string $sqlSearch
	 * @param string $orderBy
	 * @param string $limit
	 * @return array
	 */
	public static function getPhotosByGalleryId($galleryId, $sqlSearch = '', $orderBy = 'ORDER BY', $limit = '') {

		$sqlSearch = (empty($sqlSearch) ? '1=1' : $sqlSearch);

		$orderBy = ($orderBy == 'ORDER BY' ? '`ph`.`id` ASC' : $orderBy);

			$sql = "SELECT *, `ph`.`id` AS `ph_id` FROM ?f AS `g`
				INNER JOIN ?f AS `ph` ON `ph`.`ph_gallery_id` = `g`.`id`
				WHERE `g`.`id` = ? AND `g`.`deleted` = 0 AND `ph`.`deleted` = 0 AND {$sqlSearch} {$orderBy} {$limit};";

		$resultPhotos = self::$DB->q(true, $sql, self::$tableName, TAB_PHOTOS, $galleryId);
		$photos = array();
				while ($data = $resultPhotos->fetch_object()) {
				$photos[] = array(
					'ph_id' => $data->ph_id,
					'ph_original' => $data->ph_original,
					'ph_resized' => self::$PROJECT['pr_resources_live'] . $data->ph_path . DS . $data->ph_resized,
					'ph_ow' => $data->ph_ow,
					'ph_oh' => $data->ph_oh,
					'ph_rw' => $data->ph_rw,
					'ph_rh' => $data->ph_rh
				);
			}
		return $photos;
	}

	public static function getTotalItems($galleryId, $sqlSearch = ''){
		$sqlSearch = (empty($sqlSearch) ? '1=1' : $sqlSearch);

		$sql = "SELECT COUNT(*) FROM ?f AS `g`
				INNER JOIN ?f AS `ph` ON `ph`.`ph_gallery_id` = `g`.`id`
				WHERE `g`.`id` = ? AND `g`.`deleted` = 0 AND `ph`.`deleted` = 0 AND {$sqlSearch};";

		return self::$DB->selectCell($sql, self::$tableName, TAB_PHOTOS, $galleryId);
	}

	/**
	 * @static
	 * @param string $type
	 * @return array|bool
	 */
	public static function getGalleryImagesByType($type='design') {
		if (!$type) {
			return false;
		}
		$sql = "SELECT pg.*,ph.*,ph.id as photo_id FROM ?f pg INNER JOIN ?f ph ON pg.id = ph.ph_gallery_id
				WHERE pg.pg_gallery_type = ? AND pg.deleted = 0 AND ph.deleted = 0
		";
		$result = self::$DB->q($sql, self::$tableName, TAB_PHOTOS, $type);

		$dataReturn = array();
		$env = conf('ENVIRONMENT');
		while ($data = $result->fetch_object()) {
			$data->ph_original = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_name;
			$data->ph_resized = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_resized;
			$dataReturn[] = $data;
		}
		return $dataReturn;
	}

	public static function getTotalElements($sqlCondition = '1=1') {
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
		}
		return self::$DB->selectCell("SELECT COUNT(*) FROM ?f AS `g`
				LEFT JOIN ?f AS `ml` ON `ml`.`ml_gallery_id` = `g`.`id` AND `ml`.`deleted` = 0
				WHERE `g`.`deleted` = 0 AND {$sqlCondition};",self::$tableName,TAB_MEMORY_LIST);
	}

	public static function loadAllPhotoGalleries($limit, $searchSql = '', $orderBy = '') {
		if (empty($searchSql)) {
			$searchSql = '1=1';
		}
		if (empty($orderBy)) {
			$orderBy = ' ORDER BY `id` ASC ';
		}
		$sql = "SELECT pg.*, `ml`.`ml_memory_identifier` as `mem_ident` FROM ?f AS `pg`
				LEFT JOIN ?f AS `ml` ON `ml`.`ml_gallery_id` = `pg`.`id` AND `ml`.`deleted` = 0
				WHERE `pg`.`deleted` = 0 AND
                {$searchSql} {$orderBy} {$limit};
        ";
		return self::$DB->select($sql,self::$tableName, TAB_MEMORY_LIST);
	}

	/**
	 * function used for flash products manager
	 * @static
	 * @param $liIds
	 * @return bool
	 */
	public static function deleteByLiIds($liIds) {
		$liIdsList = implode(',', $liIds);
		self::$DB->q("UPDATE ?f SET `deleted` = 1 WHERE `pg_owner_id` IN( ? ) AND `deleted` = 0 AND `pg_gallery_type` = 'design' ;", self::$tableName, $liIdsList);

		return (self::$DB->mysqlModifiedRows() ? true : false);
	}

	/**
	 * @static
	 * @param $liIds
	 * @param string $type
	 * @return array
	 */
	public static function getGalleryByOwnerIdAndType($liIds, $type = 'design') {
		$liIdsList = implode(',', $liIds);
		$sql = 'SELECT id FROM ?f WHERE `pg_owner_id` IN ( ? ) AND `pg_gallery_type` = ? ;';
		$result = self::$DB->q($sql, self::$tableName, $liIdsList, $type);
		$listGalleries = array();
		while($data = $result->fetch_assoc()) {
			$listGalleries[] = $data['id'];
		}

		return $listGalleries;
	}

	/**
	 * @static
	 * @param $designs
	 * @param string $photoGalleryType
	 * @return array
	 */

	public static function getDesignsImages($designs, $photoGalleryType = 'design') {
		$sql  = "SELECT *, `ph`.`id` AS `photo_id` FROM ?f AS `pg`
					INNER JOIN ?f AS `ph` ON `pg`.`id` = `ph`.`ph_gallery_id`
					WHERE `pg`.`pg_gallery_type` = ? AND `pg`.`pg_owner_id` IN(?) AND `pg`.`deleted` = 0 AND `ph`.`deleted` = 0 ;";

		$designImages =  self::$DB->q($sql, self::$tableName, TAB_PHOTOS, $photoGalleryType, $designs);
		$returnAllDesignsImages = array();
		$env = conf('ENVIRONMENT');
		while($data = $designImages->fetch_object()) {
			$data->ph_original = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_name;
			$data->ph_resized = self::$PROJECT['pr_resources_'. $env].$data->ph_path.DS.$data->ph_resized;
			$returnAllDesignsImages[] = $data;
		}

		return $returnAllDesignsImages;
	}


}